package tianwang.model;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import tianwang.services.integrate.WordSegmentor;
import tianwang.util.FileUtil;
import tianwang.util.WordSegmentorUtil;
import tianwang.utils.punctuator.Punctuator;
import tianwang.utils.punctuator.PunctuatorStanfordImpl;
import tianwang.utils.segment.WordSegmentorStanfordImpl;

/**
 * 文章的模型
 */
public class AticleModel {
	private String name = null;
	private CAFile file = null;
	/*
	 * 文本的时间
	 */
	private String readTime = null;
	
	private String aticle = null;
	/**
	 * 保存经过分词后的全部词汇
	 */
	private List<String> words = null;
	private List<String> entityWords = null;
	private List<String> entityWhiteSpaceWords = null;
	/**
	 * 二维的列表，每句话，每个词完全分开
	 */
	private ArrayList<ArrayList<String>> punctuatorWords = null;
	
	public CAFile getFile() {
		return file;
	}

	public void setFile(CAFile file) throws Exception {
		this.file = file;
		this.aticle = FileUtil.getContent(file.getFile());
		this.name = file.getPath();
	}

	public List<String> getWords(boolean ifWhitespace) {
		if(this.aticle == null)
			return null;
		
		this.words = WordSegmentorUtil.getWordsFromText(this.aticle,ifWhitespace);
		
		return words;
	}
	
	/**
	 * 得到所有的实体单词，去除掉标点等
	 * @return
	 */
	public List<String> getEntityWords(){
		String patternString = "[a-zA-Z]+";
		Pattern p = Pattern.compile("[a-zA-Z]+");
		List<String> words = this.getWords(false);
		if(words == null){
			return null;
		}
		
		if(entityWords != null){
			return entityWords;
		}
		int len = words.size();
		entityWords = new ArrayList<String>(len);
		String entry;
		for(int i = 0; i < len; i++){
			entry = words.get(i);
			if(p.matcher(entry).find()){
				entityWords.add(entry);
			}
		}
		
		return entityWords;
	}
	
	/**
	 * 得到所有的实体单词，去除掉标点等
	 * @return
	 */
	public List<String> getEntityWordsWhiteSpace(){
		String patternString = "[a-zA-Z]+";
		Pattern p = Pattern.compile("[a-zA-Z]+");
		List<String> words = this.getWords(true);
		if(words == null){
			return null;
		}
		
		if(entityWhiteSpaceWords != null){
			return entityWhiteSpaceWords;
		}
		int len = words.size();
		entityWhiteSpaceWords = new ArrayList<String>(len);
		String entry;
		for(int i = 0; i < len; i++){
			entry = words.get(i);
			if(p.matcher(entry).find()){
				entityWhiteSpaceWords.add(entry);
			}
		}
		return entityWhiteSpaceWords;
	}
	
	/*
	 * 主要调用wanglu包下的类
	 */
	public ArrayList<ArrayList<String>> getPunctuatorWords() {
		if(this.aticle == null)
			return null;
		if(this.punctuatorWords != null)
			return this.punctuatorWords;
		
		Punctuator punctuator = new PunctuatorStanfordImpl();
		WordSegmentorStanfordImpl wordSegmentor = new WordSegmentorStanfordImpl();
		String[] sentences = punctuator.punctuate(this.aticle);
		punctuatorWords = new ArrayList<ArrayList<String>>();
		for ( int i=0; i<sentences.length; i++){
			ArrayList<String> words = wordSegmentor.segregate(sentences[i]);
			punctuatorWords.add(words);
		}
		
		return punctuatorWords;
	}
	
	public String getAticle() {
		return aticle;
	}

	public void setAticle(String aticle) {
		this.aticle = aticle;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getReadTime() {
		return readTime;
	}

	public void setReadTime(String readTime) {
		this.readTime = readTime;
	}

	
}
